// src/store/slice/comment.ts
import { createEntityAdapter, createSlice, Draft, EntityState, PayloadAction } from "@reduxjs/toolkit";
import { AppState } from "@store/index";

// 创建评论实体适配器
const commentsEntityAdaptor = createEntityAdapter({
  selectId: (state:any) => state.com_id,
});

// 创建评论列表状态切片
const { reducer: commentsReducer, actions } = createSlice({
  name: "comments",
  initialState: commentsEntityAdaptor.getInitialState(),
  reducers: {
    // 保存评论列表
    saveComments: commentsEntityAdaptor.addMany,
    // 清除评论列表
    clearComments: commentsEntityAdaptor.removeAll,
  },
});

// 用于获取评论的状态选择器
export const commentSelectors = commentsEntityAdaptor.getSelectors<AppState>(
  (state) => state.commentsReducer
);
// 导出和评论相关的 action creator 函数
export const { saveComments, clearComments } = actions;

// 导出用于管理评论列表的 reducer 函数
export default commentsReducer;